package com.woniuxy.edubureau.serviceimpl;

import com.woniuxy.commons.entity.AnswerSheet;
import com.woniuxy.commons.entity.EchartsData;
import com.woniuxy.commons.entity.ScoreLine;
import com.woniuxy.commons.entity.ScoreOfNums;
import com.woniuxy.commons.entity.wyh.GradeInfo;
import com.woniuxy.edubureau.dao.StatisticsMapper;
import com.woniuxy.edubureau.service.StatisticsService;
import com.woniuxy.edubureau.util.NumberFormateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.*;

/**
 * @author: luojiwei
 * @date: 2020/7/20 14:16
 * @description: 数据统计业务逻辑
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    private static final String NAMESPACE_SCORE="getEcharsData";
    private static final String NAMESPACE_NUM="getFurtherNums";
    private static final String NAMESPACE_RATE="getFurtherRate";
    @Autowired
    private StatisticsMapper statisticsMapper;
    @Override
    //获取各科试卷的id
    public List<Long> getTestPaper(Long paperTerm, String startDate, String endDate) {
        return statisticsMapper.getTestPaper(paperTerm,startDate,endDate);
    }
    @Cacheable(value = NAMESPACE_SCORE)
    @Override
    public List<EchartsData<Object>> getEcharsData(String sheetGrade, String year, List<Long> list) {
        List<Double> scores = getScores(sheetGrade, list);
        List<EchartsData<Object>> entryList=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        //分数段对应的人数
        ScoreLine scoreLine=statisticsMapper.getScoreLine(year);
        ScoreOfNums scoreOfNums=getScoreOfNums(scoreLine,scores);
        //计算百分比
        map.put("未达"+year+"年专科线",NumberFormateUtil.getNumFormate(scoreOfNums.getNumUnderSpecialty(),scores.size()));
        map.put("达到"+year+"年专科线",NumberFormateUtil.getNumFormate(scoreOfNums.getNumSpecialty(),scores.size()));
        map.put("达到"+year+"年本科线",NumberFormateUtil.getNumFormate(scoreOfNums.getNumRegular(),scores.size()));
        map.put("达到"+year+"年重本线",NumberFormateUtil.getNumFormate(scoreOfNums.getNumHeavy(),scores.size()));
        //RedisTemplate 设置过期时间，测试完写
        getDataList(entryList, map);
        return entryList;
    }

    //将map转list
    private void getDataList(List<EchartsData<Object>> entryList, Map<String, Object> map) {
        Set<Map.Entry<String,Object>> sets=map.entrySet();
        Iterator<Map.Entry<String,Object>> iterator=sets.iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Object> entry=iterator.next();
            String key=entry.getKey();
            Object value=entry.getValue();
            EchartsData<Object> echartsData=new EchartsData<>();
            echartsData.setKey(key);
            echartsData.setValue(value);
            entryList.add(echartsData);
        }
    }

    //获取升学人数
    @Cacheable(NAMESPACE_NUM)
    @Override
    public List<EchartsData<Object>> getFurtherNums(String sheetGrade,String year, List<Long> list) {
        List<Double> scores = getScores(sheetGrade, list);
        ScoreLine scoreLine=statisticsMapper.getScoreLine(year);
        ScoreOfNums scoreOfNums=getScoreOfNums(scoreLine,scores);
        List<EchartsData<Object>> echartsDataList=new ArrayList<>();
        echartsDataList.add(new EchartsData<>().setKey("未升学").setValue(scoreOfNums.getNumUnderSpecialty()));
        echartsDataList.add(new EchartsData<>().setKey("专科").setValue(scoreOfNums.getNumSpecialty()));
        echartsDataList.add(new EchartsData<>().setKey("本科").setValue(scoreOfNums.getNumRegular()));
        echartsDataList.add(new EchartsData<>().setKey("重本").setValue(scoreOfNums.getNumHeavy()));
        return echartsDataList;
    }

    //获取升学率
    @Cacheable(NAMESPACE_RATE)
    @Override
    public List<EchartsData<Object>> getFurtherRate(String sheetGrade,String year, List<Long> list) {
        Map<String,Object> map=new HashMap<>();
        List<Double> scores = getScores(sheetGrade, list);
        ScoreLine scoreLine=statisticsMapper.getScoreLine(year);
        ScoreOfNums scoreOfNums=getScoreOfNums(scoreLine,scores);
        List<EchartsData<Object>> entryList=new ArrayList<>();

        map.put("未升学",NumberFormateUtil.getNumFormate(scoreOfNums.getNumUnderSpecialty(),scores.size()));
        map.put("预估专科升学率",NumberFormateUtil.getNumFormate(scoreOfNums.getNumSpecialty(),scores.size()));
        map.put("预估本科升学率",NumberFormateUtil.getNumFormate(scoreOfNums.getNumRegular(),scores.size()));
        map.put("预估重本升学率",NumberFormateUtil.getNumFormate(scoreOfNums.getNumHeavy(),scores.size()));
        getDataList(entryList,map);
        return entryList;
    }

    //获取年级信息
    @Override
    public List<GradeInfo> getGradeInfos() {
        return statisticsMapper.getGradeInfos();
    }

    //分数段对应的人数
    private ScoreOfNums getScoreOfNums(ScoreLine scoreLine,List<Double> scores){
        ScoreOfNums scoreOfNums=new ScoreOfNums();
        int numUnderSpecialty=0;
        int numSpecialty=0;
        int numRegular=0;
        int numHeavy=0;
        double specialty=scoreLine.getSpecialty();
        double regular=scoreLine.getRegular();
        double heavy=scoreLine.getHeavy();
        for (Double score:scores) {
            if (0<=score&&score<=specialty){
                numUnderSpecialty++;
            }
            else if (score<=regular){
                numSpecialty++;
            }
            else if (score<=heavy){
                numRegular++;
            }else {
                numHeavy++;
            }
        }
        scoreOfNums.setNumUnderSpecialty(numUnderSpecialty);
        scoreOfNums.setNumSpecialty(numSpecialty);
        scoreOfNums.setNumRegular(numRegular);
        scoreOfNums.setNumHeavy(numHeavy);
        return scoreOfNums;
    }
    //根据条件获取分数
    private List<Double> getScores(String sheetGrade, List<Long> list) {
        Set<Long> sheetStudentNos=new HashSet<>();
        List<Double> scores=new ArrayList<>();
        List<AnswerSheet> answerSheets=new ArrayList<>();
        for (Long sheetPaperId : list) {
            //根据年级，试卷id获取答题卡信息
            answerSheets= statisticsMapper.getAnswerSheet(sheetGrade,sheetPaperId);
        }
        //获取学号，set去重
        for (AnswerSheet answerSheet:answerSheets) {
            sheetStudentNos.add(answerSheet.getSheetStudentNo());
        }
        // 再根据学号查出成绩，再算总分
        for (Long sheetStudentNo:sheetStudentNos) {
            List<Double> sheetScores=statisticsMapper.getScore(sheetStudentNo);
            //每个学号对应的总分
            double score=0;
            for (Double sheetScore:sheetScores) {
                score+=sheetScore;
            }
            scores.add(score);
        }
        return scores;
    }

}
